package View;

import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import Model.*;
import Controller.*;


/**
 * Visualization in multiple mode class
 *
 * @author Jose Filipe M Santos 7277502
 * @author Vesgo nao vai esquecer o nome
 */
public class MultipleModeWindow extends JFrame{
    
    private MultipleGame match0;
    private MultipleGame match1;
    private MultipleGame match2;
    private MultipleGame match3;
    private MultipleGame match4;


    private JLabel team0;
    private JLabel team1;
    private JLabel team2;
    private JLabel team3;
    private JLabel team4;

    private JButton simulate;
    private JButton scale0;
    private JButton scale1;
    private JButton scale2;
    private JButton scale3;
    private JButton scale4;

    public MultipleModeWindow(){


        this.setSize(600, 245);
        this.setResizable(false);

        JFileChooser filechooser = new JFileChooser();
            FileFilter filter = new FileNameExtensionFilter("Simulator File", "txt");
            filechooser.setFileFilter(filter);

            int returnval = filechooser.showOpenDialog(this);

            if(returnval == JFileChooser.APPROVE_OPTION){
                GameData N = new GameData();
                N.loadFile(filechooser.getSelectedFile().getPath());
                this.match0 = new MultipleGame(new java.util.Date(), N.getLoadedTeam1(), N.getLoadedTeam2());

            }


            returnval = filechooser.showOpenDialog(this);

            if(returnval == JFileChooser.APPROVE_OPTION){
                GameData N = new GameData();
                N.loadFile(filechooser.getSelectedFile().getPath());
                this.match1 = new MultipleGame(new java.util.Date(), N.getLoadedTeam1(), N.getLoadedTeam2());

            }

            returnval = filechooser.showOpenDialog(this);

            if(returnval == JFileChooser.APPROVE_OPTION){
                GameData N = new GameData();
                N.loadFile(filechooser.getSelectedFile().getPath());
                this.match2 = new MultipleGame(new java.util.Date(), N.getLoadedTeam1(), N.getLoadedTeam2());

            }

            returnval = filechooser.showOpenDialog(this);

            if(returnval == JFileChooser.APPROVE_OPTION){
                GameData N = new GameData();
                N.loadFile(filechooser.getSelectedFile().getPath());
                this.match3 = new MultipleGame(new java.util.Date(), N.getLoadedTeam1(), N.getLoadedTeam2());

            }

            returnval = filechooser.showOpenDialog(this);

            if(returnval == JFileChooser.APPROVE_OPTION){
                GameData N = new GameData();
                N.loadFile(filechooser.getSelectedFile().getPath());
                this.match4 = new MultipleGame(new java.util.Date(), N.getLoadedTeam1(), N.getLoadedTeam2());

            }




        team0 = new JLabel(match0.getscore());
        team1 = new JLabel(match1.getscore());
        team2 = new JLabel(match2.getscore());
        team3 = new JLabel(match3.getscore());
        team4 = new JLabel(match4.getscore());

        team0.setSize(490, 30);
        team0.setLocation(10, 10);
        team1.setSize(490, 30);
        team1.setLocation(10, 40);
        team2.setSize(490, 30);
        team2.setLocation(10, 70);
        team3.setSize(490, 30);
        team3.setLocation(10, 100);
        team4.setSize(490, 30);
        team4.setLocation(10, 130);

        
        scale0 = new JButton("Scale");
        scale0.setSize(100, 30);
        scale0.setLocation(490, 10);
        scale1 = new JButton("Scale");
        scale1.setSize(100, 30);
        scale1.setLocation(490, 40);
        scale2 = new JButton("Scale");
        scale2.setSize(100, 30);
        scale2.setLocation(490, 70);
        scale3 = new JButton("Scale");
        scale3.setSize(100, 30);
        scale3.setLocation(490, 100);
        scale4 = new JButton("Scale");
        scale4.setSize(100, 30);
        scale4.setLocation(490, 130);
        simulate = new JButton("Simulate");
        simulate.setSize(110, 40);
        simulate.setLocation(240, 170);
        simulate.setEnabled(false);
        
        this.add(team0);
        this.add(team1);
        this.add(team2);
        this.add(team3);
        this.add(team4);
        this.add(scale0);
        this.add(scale1);
        this.add(scale2);
        this.add(scale3);
        this.add(scale4);
        this.add(simulate);
        this.add(new JLabel());

        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
        this.setVisible(true);



        this.scale0.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Scale(match0.getHomeTeam(), match0.getVisitorTeam());
                if(isReady(match1, match2, match3, match4))
                    simulate.setEnabled(true);
            }
        });

        this.scale1.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Scale(match1.getHomeTeam(), match1.getVisitorTeam());
                if(isReady(match0, match2, match3, match4))
                    simulate.setEnabled(true);
            }
        });

        this.scale2.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Scale(match2.getHomeTeam(), match2.getVisitorTeam());
                if(isReady(match0, match1, match3, match4))
                    simulate.setEnabled(true);
            }
        });

        this.scale3.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Scale(match3.getHomeTeam(), match3.getVisitorTeam());
                if(isReady(match0, match1, match2, match4))
                    simulate.setEnabled(true);
            }
        });

        this.scale4.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Scale(match4.getHomeTeam(), match4.getVisitorTeam());
                if(isReady(match0, match1, match2, match3))
                    simulate.setEnabled(true);
            }
        });

        simulate.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                match0 = new MultipleGame(new java.util.Date(), match0.getHomeTeam(), match0.getVisitorTeam());
                match1 = new MultipleGame(new java.util.Date(), match1.getHomeTeam(), match1.getVisitorTeam());
                match2 = new MultipleGame(new java.util.Date(), match2.getHomeTeam(), match2.getVisitorTeam());
                match3 = new MultipleGame(new java.util.Date(), match3.getHomeTeam(), match3.getVisitorTeam());
                match4 = new MultipleGame(new java.util.Date(), match4.getHomeTeam(), match4.getVisitorTeam());
                match0.run();
                match1.run();
                match2.run();
                match3.run();
                match4.run();
                setWindow();
            }
        });



    }

    public void Scale(final Team t1, final Team t2){

        final JFrame window = new JFrame("Scale "+t1.getName() +" "+t2.getName());
        final JButton ok = new JButton("Ready");
        window.setSize(600, 400);
        window.setResizable(false);

        ok.setSize(100, 30);
        ok.setLocation(250, 330);
        if(t1.isReady() && t2.isReady())
            ok.setEnabled(true);
        else
            ok.setEnabled(false);

        JCheckBox t1p0 = new JCheckBox("Gk " + t1.getGkPlayers().get(0).getName() + " [" +t1.getGkPlayers().get(0).getHability() +"]" + "[0]");
        if(t1.isStarting(0)){
            t1p0.setSelected(true);
        }
        JCheckBox t1p1 = new JCheckBox("Gk " + t1.getGkPlayers().get(1).getName() + " [" +t1.getGkPlayers().get(1).getHability() +"]" + "[0]");
        if(t1.isStarting(1)){
            t1p1.setSelected(true);
        }
        JCheckBox t1p2 = new JCheckBox("Def " + t1.getDefensePlayers().get(0).getName() + " [" +t1.getDefensePlayers().get(0).getHability() +"]["+((PlayerDef)(t1.getDefensePlayers().get(0))).getGoals() + "]");
        if(t1.isStarting(6)){
            t1p2.setSelected(true);
        }
        JCheckBox t1p3 = new JCheckBox("Def " + t1.getDefensePlayers().get(1).getName() + " [" +t1.getDefensePlayers().get(1).getHability() +"]["+((PlayerDef)(t1.getDefensePlayers().get(1))).getGoals() + "]");
        if(t1.isStarting(7)){
            t1p3.setSelected(true);
        }
        JCheckBox t1p4 = new JCheckBox("Def " + t1.getDefensePlayers().get(2).getName() + " [" +t1.getDefensePlayers().get(2).getHability() +"]["+((PlayerDef)(t1.getDefensePlayers().get(2))).getGoals() + "]");
        if(t1.isStarting(8)){
            t1p4.setSelected(true);
        }
        JCheckBox t1p5 = new JCheckBox("Def " + t1.getDefensePlayers().get(3).getName() + " [" +t1.getDefensePlayers().get(3).getHability() +"]["+((PlayerDef)(t1.getDefensePlayers().get(3))).getGoals() + "]");
        if(t1.isStarting(9)){
            t1p5.setSelected(true);
        }
        JCheckBox t1p6 = new JCheckBox("Fwd " + t1.getAttackPlayers().get(0).getName() + " [" +t1.getAttackPlayers().get(0).getHability() +"]["+((PlayerAt)(t1.getAttackPlayers().get(0))).getGoals() + "]");
        if(t1.isStarting(2)){
            t1p6.setSelected(true);
        }
        JCheckBox t1p7 = new JCheckBox("Fwd " + t1.getAttackPlayers().get(1).getName() + " [" +t1.getAttackPlayers().get(1).getHability() +"]["+((PlayerAt)(t1.getAttackPlayers().get(1))).getGoals() + "]");
        if(t1.isStarting(3)){
            t1p7.setSelected(true);
        }
        JCheckBox t1p8 = new JCheckBox("Fwd " + t1.getAttackPlayers().get(2).getName() + " [" +t1.getAttackPlayers().get(2).getHability() +"]["+((PlayerAt)(t1.getAttackPlayers().get(2))).getGoals() + "]");
        if(t1.isStarting(4)){
            t1p8.setSelected(true);
        }
        JCheckBox t1p9 = new JCheckBox("Fwd " + t1.getAttackPlayers().get(3).getName() + " [" +t1.getAttackPlayers().get(3).getHability() +"]["+((PlayerAt)(t1.getAttackPlayers().get(3))).getGoals() + "]");
        if(t1.isStarting(5)){
            t1p9.setSelected(true);
        }


        JCheckBox t2p0 = new JCheckBox("Gk " + t2.getGkPlayers().get(0).getName() + " [" +t2.getGkPlayers().get(0).getHability() +"]" + "[0]");
        if(t2.isStarting(0)){
            t2p0.setSelected(true);
        }
        JCheckBox t2p1 = new JCheckBox("Gk " + t2.getGkPlayers().get(1).getName() + " [" +t2.getGkPlayers().get(1).getHability() +"]" + "[0]");
        if(t2.isStarting(1)){
            t2p1.setSelected(true);
        }
        JCheckBox t2p2 = new JCheckBox("Def " + t2.getDefensePlayers().get(0).getName() + " [" +t2.getDefensePlayers().get(0).getHability() +"]["+((PlayerDef)(t2.getDefensePlayers().get(0))).getGoals() + "]");
        if(t2.isStarting(6)){
            t2p2.setSelected(true);
        }
        JCheckBox t2p3 = new JCheckBox("Def " + t2.getDefensePlayers().get(1).getName() + " [" +t2.getDefensePlayers().get(1).getHability() +"]["+((PlayerDef)(t2.getDefensePlayers().get(1))).getGoals() + "]");
        if(t2.isStarting(7)){
            t2p3.setSelected(true);
        }
        JCheckBox t2p4 = new JCheckBox("Def " + t2.getDefensePlayers().get(2).getName() + " [" +t2.getDefensePlayers().get(2).getHability() +"]["+((PlayerDef)(t2.getDefensePlayers().get(2))).getGoals() + "]");
        if(t2.isStarting(8)){
            t2p4.setSelected(true);
        }
        JCheckBox t2p5 = new JCheckBox("Def " + t2.getDefensePlayers().get(3).getName() + " [" +t2.getDefensePlayers().get(3).getHability() +"]["+((PlayerDef)(t2.getDefensePlayers().get(3))).getGoals() + "]");
        if(t2.isStarting(9)){
            t2p5.setSelected(true);
        }
        JCheckBox t2p6 = new JCheckBox("Fwd " + t2.getAttackPlayers().get(0).getName() + " [" +t2.getAttackPlayers().get(0).getHability() +"]["+((PlayerAt)(t2.getAttackPlayers().get(0))).getGoals() + "]");
        if(t2.isStarting(2)){
            t2p6.setSelected(true);
        }
        JCheckBox t2p7 = new JCheckBox("Fwd " + t2.getAttackPlayers().get(1).getName() + " [" +t2.getAttackPlayers().get(1).getHability() +"]["+((PlayerAt)(t2.getAttackPlayers().get(1))).getGoals() + "]");
        if(t2.isStarting(3)){
            t2p7.setSelected(true);
        }
        JCheckBox t2p8 = new JCheckBox("Fwd " + t2.getAttackPlayers().get(2).getName() + " [" +t2.getAttackPlayers().get(2).getHability() +"]["+((PlayerAt)(t2.getAttackPlayers().get(2))).getGoals() + "]");
        if(t2.isStarting(4)){
            t2p8.setSelected(true);
        }
        JCheckBox t2p9 = new JCheckBox("Fwd " + t2.getAttackPlayers().get(3).getName() + " [" +t2.getAttackPlayers().get(3).getHability() +"]["+((PlayerAt)(t2.getAttackPlayers().get(3))).getGoals() + "]");
        if(t2.isStarting(5)){
            t2p9.setSelected(true);
        }

        
        t1p0.setSize(235, 30);
        t1p1.setSize(235, 30);
        t1p2.setSize(235, 30);
        t1p3.setSize(235, 30);
        t1p4.setSize(235, 30);
        t1p5.setSize(235, 30);
        t1p6.setSize(235, 30);
        t1p7.setSize(235, 30);
        t1p8.setSize(235, 30);
        t1p9.setSize(235, 30);

        t2p0.setSize(235, 30);
        t2p1.setSize(235, 30);
        t2p2.setSize(235, 30);
        t2p3.setSize(235, 30);
        t2p4.setSize(235, 30);
        t2p5.setSize(235, 30);
        t2p6.setSize(235, 30);
        t2p7.setSize(235, 30);
        t2p8.setSize(235, 30);
        t2p9.setSize(235, 30);

        t1p0.setLocation(10, 10);
        t1p1.setLocation(10, 40);
        t1p2.setLocation(10, 70);
        t1p3.setLocation(10, 100);
        t1p4.setLocation(10, 130);
        t1p5.setLocation(10, 160);
        t1p6.setLocation(10, 190);
        t1p7.setLocation(10, 220);
        t1p8.setLocation(10, 250);
        t1p9.setLocation(10, 280);

        t2p0.setLocation(300, 10);
        t2p1.setLocation(300, 40);
        t2p2.setLocation(300, 70);
        t2p3.setLocation(300, 100);
        t2p4.setLocation(300, 130);
        t2p5.setLocation(300, 160);
        t2p6.setLocation(300, 190);
        t2p7.setLocation(300, 220);
        t2p8.setLocation(300, 250);
        t2p9.setLocation(300, 280);

        t1p0.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(0);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p1.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(1);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p2.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(6);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p3.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(7);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p4.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(8);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p5.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(9);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p6.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(2);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p7.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(3);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p8.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(4);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t1p9.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.ChangeStatus(5);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });


        t2p0.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(0);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p1.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(1);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p2.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(6);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p3.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(7);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p4.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(8);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p5.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(9);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p6.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(2);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p7.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(3);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p8.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(4);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        t2p9.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t2.ChangeStatus(5);
                        if(t1.isReady() && t2.isReady())
                            ok.setEnabled(true);
                        else
                            ok.setEnabled(false);
            }
        });

        ok.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                window.setVisible(false);

            }
        });



        window.add(ok);

        window.add(t1p0);
        window.add(t1p1);
        window.add(t1p2);
        window.add(t1p3);
        window.add(t1p4);
        window.add(t1p5);
        window.add(t1p6);
        window.add(t1p7);
        window.add(t1p8);
        window.add(t1p9);

        window.add(t2p0);
        window.add(t2p1);
        window.add(t2p2);
        window.add(t2p3);
        window.add(t2p4);
        window.add(t2p5);
        window.add(t2p6);
        window.add(t2p7);
        window.add(t2p8);
        window.add(t2p9);
        window.add(new JLabel());

        window.setVisible(true);


    }

    boolean isReady(MultipleGame m0, MultipleGame m1, MultipleGame m2, MultipleGame m3){

        boolean ma0, ma1, ma2, ma3, ma4;

        if(m0.getHomeTeam().isReady() && m0.getVisitorTeam().isReady())
            ma0 = true;
        else
            ma0 = false;
        if(m1.getHomeTeam().isReady() && m1.getVisitorTeam().isReady())
            ma1 = true;
        else
            ma1 = false;
        if(m2.getHomeTeam().isReady() && m2.getVisitorTeam().isReady())
            ma2 = true;
        else
            ma2 = false;
        if(m3.getHomeTeam().isReady() && m3.getVisitorTeam().isReady())
            ma3 = true;
        else
            ma3 = false;

        return(ma0 && ma1 && ma2 && ma3);

    }

    public void setWindow(){

        team0.setText(match0.getscore());
        team1.setText(match1.getscore());
        team2.setText(match2.getscore());
        team3.setText(match3.getscore());
        team4.setText(match4.getscore());


    }



}
